package com.uyayo.oms.system.util;

import com.uyayo.framework.common.util.ValidateUtil;
import com.uyayo.framework.common.util.io.FileUtil;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;


/**
 * 下载文件
 * 创建人：FH 创建时间：2014年12月23日
 * @version
 */
public class FileDownload {

	/**
	 * @param response 
	 * @param filePath		//文件完整路径(包括文件名和扩展名)
	 * @param fileName		//下载后看到的文件名
	 * @return  文件名
	 */
	public static void fileDownload(final HttpServletResponse response, String filePath, String fileName) throws Exception{  
		     
	    byte[] data = FileUtil.toByteArray2(filePath);  
	    //fileName = URLEncoder.encode(fileName, "UTF-8");  
	    response.reset();  
	    response.setHeader("Content-Disposition", "attachment; fileName=\"" + new String(fileName.getBytes("UTF-8"),"ISO8859-1") + "\"");  
	    response.addHeader("Content-Length", "" + data.length);  
	    response.setContentType("application/octet-stream;charset=UTF-8");  
	    OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());  
	    outputStream.write(data);  
	    outputStream.flush();  
	    outputStream.close();
	    response.flushBuffer();
	    
	}
	
	
	
	public static void fileDownload_UTF8(final HttpServletResponse response, String filePath, String fileName) throws Exception{  
	     
	    byte[] data = FileUtil.toByteArray2(filePath);  
	    response.reset();  
	    response.setHeader("Content-Disposition", "attachment; fileName=\"" + fileName+ "\"");  
	    response.addHeader("Content-Length", "" + data.length);  
	    response.setContentType("application/octet-stream;charset=UTF-8");  
	    OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());  
	    outputStream.write(data);  
	    outputStream.flush();  
	    outputStream.close();
	    response.flushBuffer();
	    
	} 
	
	
	
	/**
	 * getResponse()
	 * filepath 数据库表里保存的路径path
	 * reallyfileName 数据库表里保存的filename，带后缀名，下载后默认的文件名
	 * */
	public static boolean downloadFile(HttpServletResponse response,String filePath,String fileName) throws Exception{
		boolean check = true;
		File file;
		if (ValidateUtil.isNotEmpty(filePath)) {
			file = new File(filePath);
			if (file.exists()) {
				byte[] bs = new byte[1024];
				ByteBuffer byteBuff = ByteBuffer.allocate(1024);
				FileChannel channel = new RandomAccessFile(file, "r").getChannel();
				response.setContentType("application/x-download;charset=UTF-8");
				if(fileName==null||"".equals(fileName)){
					fileName=file.getName();
				}
				//new String(fileName.getBytes("gb2312"),"ISO8859-1"))
				response.setHeader("Content-Disposition","attachment; fileName=" + fileName);
				ServletOutputStream os = response.getOutputStream();
				while (channel.read(byteBuff) != -1) {
					int size = byteBuff.position();
					byteBuff.rewind();
					byteBuff.get(bs);
					os.write(bs, 0, size);
					byteBuff.clear();
				}
				os.flush();
				os.close();
				channel.close();
			}else{
				check = false;
				throw new Exception("下载的文件不存在！");
			}
		}
		file=null;
        return check;
	}

}
